package com.xungou.sender.controller;

import com.xungou.receiver.pojo.Receiver;
import com.xungou.sender.pojo.Sender;
import com.xungou.sender.service.ISenderService;
import com.xungou.shop.dao.IShop;
import com.xungou.shop.pojo.Shop;
import com.xungou.shop.service.IShopService;
import com.xungou.utils.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sender")
public class SenderController {
    @Resource
    private ISenderService senderService;
    @Resource
    private IShopService shopService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(Sender sender, @RequestParam Map<String, String> params) {
        Shop shop = shopService.getShopByUserId(UserUtil.getCurrentUser().getId());
        sender.setShop(shop.getId());
        int flag = senderService.insert(sender);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Receiver receiver) {
        int flag = senderService.deleteByPrimaryKey(receiver.getId());
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        return result;
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Sender> list(@RequestParam Map<String, Object> params){
        Shop shop = shopService.getShopByUserId(UserUtil.getCurrentUser().getId());
        params.put("shopId",shop.getId());
        List<Sender> result = senderService.list(params);
        return result;
    }

}